
package com.jlcloud.core.log.publisher;

import com.jlcloud.core.log.constant.EventConstant;
import com.jlcloud.core.log.event.BusinessLogEvent;
import com.jlcloud.core.log.model.LogBusiness;
import com.jlcloud.core.log.utils.LogAbstractUtil;
import com.jlcloud.core.tool.utils.SpringUtil;
import com.jlcloud.core.tool.utils.WebUtil;
import jakarta.servlet.http.HttpServletRequest;

import java.util.HashMap;
import java.util.Map;

/**
 * JOBSLINK日志信息事件发送
 *
 * @author liyan
 */
public class BusinessLogPublisher {

	public static void publishEvent(String level, String id, String data) {
		HttpServletRequest request = WebUtil.getRequest();
		LogBusiness log = new LogBusiness();
		Thread thread = Thread.currentThread();
		StackTraceElement[] trace = thread.getStackTrace();
		if (trace.length > 3) {
			log.setMethodClass(trace[3].getClassName());
			log.setMethodName(trace[3].getMethodName());
		}
		LogAbstractUtil.addRequestInfoToLog(request, log);
		Map<String, Object> event = new HashMap<>(16);
		event.put(EventConstant.EVENT_LOG, log);
		SpringUtil.publishEvent(new BusinessLogEvent(event));
	}

	public static void publishEvent(LogBusiness log) {
		HttpServletRequest request = WebUtil.getRequest();
		Thread thread = Thread.currentThread();
		StackTraceElement[] trace = thread.getStackTrace();
		if (trace.length > 3) {
			log.setMethodClass(trace[3].getClassName());
			log.setMethodName(trace[3].getMethodName());
		}
		LogAbstractUtil.addRequestInfoToLog(request, log);
		Map<String, Object> event = new HashMap<>(16);
		event.put(EventConstant.BUSINESS_LOG, log);
		SpringUtil.publishEvent(new BusinessLogEvent(event));
	}
}
